﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Text.RegularExpressions;
using AuthorData.Entities;

/// <summary>
/// Summary description for LinkFinder
/// </summary>
public static class LinkFinder
{
    public struct LinkItem
    {
        public string Href;
        public string Text;
        public override string ToString()
        {
            return Href + "\n\t" + Text;
        }
    }

    #region parse a element
    public static List<LinkItem> Find(string file)
    {
        List<LinkItem> list = new List<LinkItem>();

        // 1.
        // Find all matches in file.
        MatchCollection m1 = Regex.Matches(file, @"(<a.*?>.*?</a>)", RegexOptions.Singleline);

        // 2.
        // Loop over each match.
        foreach (Match m in m1)
        {
            string value = m.Groups[1].Value;
            LinkItem i = new LinkItem();
            // 3.
            // Get href attribute.
            Match m2 = Regex.Match(value, @"href=\""(.*?)\""",
            RegexOptions.Singleline);
            if (m2.Success)
            {
                i.Href = "http://www.brainyquote.com" + m2.Groups[1].Value;
            }
            else
                i.Href = string.Empty;

            // 4.
            // Remove inner tags from text.
            string t = Regex.Replace(value, @"\s*<.*?>\s*", "", RegexOptions.Singleline);
            i.Text = t;
            list.Add(i);
        }
        return list;
    }

    public static List<LinkItem> FindAuthor(string file)
    {
        List<LinkItem> list = new List<LinkItem>();
        MatchCollection m1 = Regex.Matches(file, @"(<a.*?>.*?</a>)", RegexOptions.Singleline);
        foreach (Match m in m1)
        {
            string value = m.Groups[1].Value;
            LinkItem i = new LinkItem();
            Match m2 = Regex.Match(value, @"href=\""(.*?)\""",RegexOptions.Singleline);
            if (m2.Success)
            {
                i.Href = m2.Groups[1].Value;
                Match match = Regex.Match(i.Href, @"\d+", RegexOptions.Singleline);
                if (match.Success)
                {
                    i.Href = match.Value;
                }
                else
                    i.Href = string.Empty;
            }
            string t = Regex.Replace(value, @"\s*<.*?>\s*", "", RegexOptions.Singleline);
            i.Text = t;
            list.Add(i);
        }
        return list;
    }
    #endregion

    public static void ParseMainContents(string file, ref List<Quotations> lstQuotations, ref List<Authors> lstAuthors,ref List<Topics> lstTopics,ref List<Tags> lstTags)
    {
        string strRegex = string.Empty;
        //style=\""(.+?)\""
        strRegex = @"(<div class=\""masonryitem boxy bqQt\"">.*?</div>)";
        //file = Regex.Replace(file, strRegex,"");
        MatchCollection m1 = Regex.Matches(file, strRegex, RegexOptions.Singleline);
        foreach (Match m in m1)
        {
            Quotations objQuotations = new Quotations();
            Authors objAuthors = new Authors();
            Topics objTopics = new Topics();
            Tags objTags = new Tags();

            string value = m.Groups[1].Value;
            objQuotations.Quotes = ParseQoutes(value);
            objAuthors = ParseAuthor(value);
            ParseTopic(value, objTopics, objTags);
            lstQuotations.Add(objQuotations);
            lstAuthors.Add(objAuthors);
            lstTopics.Add(objTopics);
            lstTags.Add(objTags);
        }
    }
    
    #region comment
    /// <summary>
    /// parse qoutes and author
    /// </summary>
    /// <param name="file"></param>
    /// <param name="quotation"></param>
    /// <param name="author"></param>
    /// <param name="topic"></param>
    /// <param name="tag"></param>
    /*public static void ParseContent(string file, ref Quotations quotation, ref Authors author)
    {
        string strRegex = string.Empty; //@"(<div class=" + "bq_fq bq_fq_lrg" + "style=" + "margin:0px" + ">.*?</div>)";
        strRegex = @"(<div class=\""boxyPaddingBig\"">.*?</div>)";
        Match m = Regex.Match(file, strRegex, RegexOptions.Singleline);
        string value = m.Groups[1].Value;
        quotation.Quotes = ParseQoutes(value);
        List<LinkItem> lst = ParseAuthor(value);
        if (lst != null || lst.Count > 0)
        {
            author.RefId = Convert.ToInt32(lst[0].Href);
            author.AuthorName = lst[0].Text.Trim();
        }

    }*/
    /*public static void ParseContents(string file, ref Quotations quotation, ref Authors author, Topics topic, Tags tag)
    {
        string strRegex = string.Empty; //@"(<div class=" + "bq_fq bq_fq_lrg" + "style=" + "margin:0px" + ">.*?</div>)";
        strRegex = @"(<div class=\""boxyPaddingBig\"">.*?</div>)";
        MatchCollection m1 = Regex.Matches(file, strRegex, RegexOptions.Singleline);
        foreach (Match m in m1)
        {
            Quotations objQuotations = new Quotations();
            Authors objAuthors = new Authors();
            Topics objTopics = new Topics();
            Tags objTags = new Tags();

            string value = m.Groups[1].Value;
            objQuotations.Quotes = ParseQoutes(value);
            objAuthors.AuthorName = ParseAuthor(value);
        }
    }*/
    #endregion

    #region parse qoutes and author
    public static string ParseQoutes(string file)
    {
        string strRegex = string.Empty; //@"(<div class=" + "bq_fq bq_fq_lrg" + "style=" + "margin:0px" + ">.*?</div>)";
        strRegex = @"(<span class=\""bqQuoteLink\"">.*?</span>)";
        Match m = Regex.Match(file, strRegex, RegexOptions.Singleline);
        if (m.Success)
        {
            string value = m.Groups[1].Value;
            if (!string.IsNullOrEmpty(value))
            {
                List<LinkItem> lst = FindAuthor(value);
                if (lst != null || lst.Count > 0)
                    return lst[0].Text;
            }
        }
        return string.Empty;
    }
    public static Authors ParseAuthor(string file)
    {
        Authors author = new Authors();
        string strRegex = string.Empty; //@"(<div class=" + "bq_fq bq_fq_lrg" + "style=" + "margin:0px" + ">.*?</div>)";
        strRegex = @"(<span class=\""bodybold\"">.*?</span>)";
        Match m = Regex.Match(file, strRegex, RegexOptions.Singleline);
        if (m.Success)
        {
            string value = m.Groups[1].Value;
            if (!string.IsNullOrEmpty(value))
            {
                List<LinkItem> lst = FindAuthor(value);
                if (lst == null || lst.Count == 0)
                    return null;
                if (!string.IsNullOrEmpty(lst[0].Href))
                    author.RefId = Convert.ToInt32(lst[0].Href);
                author.AuthorName = lst[0].Text;
            }
        }
        return author;
    }
    #endregion

    #region parse topic
    public static void ParseTopic(string file, Topics topic, Tags tag)
    {
        if (!file.Contains(@"class=\""body boxyRelated bqBlackLink\""")) ;
            return;
        //style=\""(.+?)\"
        string strRegex = string.Empty; //@"(<div class=" + "bq_fq bq_fq_lrg" + "style=" + "margin:0px" + ">.*?</div>)";
        strRegex = @"(<div class=\""body boxyRelated bqBlackLink\"">.*?</div>)";
        Match m = Regex.Match(file, strRegex, RegexOptions.Singleline);
        string value = m.Groups[1].Value;
        List<LinkItem> items = Find(value);
        if (items != null && items.Count > 0)
        {
            foreach (var item in items)
            {
                string strTopic = item.Href;
                if (strTopic.Contains("topics"))
                    topic.TopicName += item.Text.Trim() + ",";
                else if (strTopic.Contains("keywords"))
                    tag.TagName += item.Text.Trim() + ",";
            }
        }
    }
    #endregion

}